<%@page import="com.assg2.dao.DaoViewer"%>
<%@page import="com.assg2.dao.DaoMovie"%>
<%@page import="com.movie.Recommend"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.movie.Viewer"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.movie.Movie"%>
<%@page import="com.enterprise.web.*"%>
<%@page contentType="text/html"%>

<jsp:useBean id="ListMovies" class="com.assg2.dao.DaoMovie" />
<jsp:useBean id="Movie" class="com.movie.Movie" />
<jsp:useBean id="Viewer" class="com.movie.Viewer" />


<%@include file="headerv.html"%>

<h3>Welcome to <b>Cinema World,</b> <span> This are the movies now showing</span></h3>

<%
    String usertype = (String) session.getAttribute("usertype");

    if ((session.getAttribute("user") != null) && (usertype.matches("viewer"))) {
        Viewer = (Viewer) session.getAttribute("user");
        if (!Viewer.getReviews().isEmpty()) {
            List<Movie> recommended = ListMovies.recomendedmovies(Viewer.getIdviewer());
            if (recommended != null) {
                out.println("<CENTER><h1>Recomended</h1></CENTER>");
                for (Movie movie1 : recommended) {
                    out.println("<br><center><a href='dispatcher?operation=movieinfo&movieid=" + Integer.toString(movie1.getIdmovie()) + "'>" + movie1.getTitle() + "</a></center>");
                }
            }
        }
        out.println("<br>");


        List<Movie> showing = ListMovies.listShowing();
        List<Movie> coming = ListMovies.listComing();


        out.println("<CENTER><h1>Showing</h1></CENTER>");
        for (int i = 0; i < showing.size(); i++) {
            Movie = showing.get(i);
            out.println("<CENTER><table width='700px'>");
            out.println("<tr>");
            out.println("<td colspan='2' height='50px'>");
            out.println("<CENTER><h1><a href='dispatcher?operation=movieinfo&movieid=" + Integer.toString(Movie.getIdmovie()) + "'>" + Movie.getTitle() + "</a></h1></CENTER>");
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");

            out.println("<td valign='top'>");
            out.println("<CENTER><img width='100px' src='images/posters/" + Movie.getPoster() + "' /></CENTER>");
            out.println("</td>");


            out.println("<td valign='top'>");
            out.println("<CENTER><table width='550px'>");

            out.println("<tr>");
            out.println("<td >");
            out.println("<b>Rating: </b>" + Movie.getAgerating());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("<b>Director: </b>" + Movie.getDirector());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td align='justify'>");
            out.println("<b>Sypnosis: </b>" + Movie.getSynopsis());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("<b>Avg Review: </b>" + ListMovies.movierating(Movie.getIdmovie()));
            out.println("</td>");
            out.println("</tr>");

            out.println("</table></CENTER>");
            out.println("</td>");
            out.println("</tr>");

            DaoViewer dv = new DaoViewer();
            Viewer v = (Viewer) dv.findById(Viewer.getIdviewer());
            Iterator iterator = v.getRecommended().iterator();
            out.println("<tr><td colspan='2' align='center'><table width='699px' BORDER='5'>");
            out.println("<tr><td>Recommended by:");
            while (iterator.hasNext()) {
                Recommend recommended = (Recommend) iterator.next();
                if (recommended.getMovie().getTitle().matches(Movie.getTitle())) {
                    out.println(recommended.getViewer().getFirstname() + " " + recommended.getViewer().getLastname() + ";");
                }
            }
            out.println("</td></tr></table></td></tr>");

            out.println("</table></CENTER>");


        }


        out.println("<br>");
        out.println("<CENTER><h1>Coming</h1></CENTER>");
        for (int i = 0; i < coming.size(); i++) {
            Movie = coming.get(i);
            out.println("<CENTER><table width='700px'>");
            out.println("<tr>");
            out.println("<td colspan='2' height='50px'>");
            out.println("<CENTER><h1><a href='dispatcher?operation=movieinfo&movieid=" + Integer.toString(Movie.getIdmovie()) + "'>" + Movie.getTitle() + "</a></h1></CENTER>");
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");

            out.println("<td valign='top'>");
            out.println("<img width='100px' src='images/posters/" + Movie.getPoster() + "' />");
            out.println("</td>");


            out.println("<td valign='top'>");
            out.println("<CENTER><table width='550px'>");

            out.println("<tr>");
            out.println("<td >");
            out.println("<b>Rating: </b>" + Movie.getAgerating());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td>");
            out.println("<b>Director: </b>" + Movie.getDirector());
            out.println("</td>");
            out.println("</tr>");

            out.println("<tr>");
            out.println("<td align='justify'>");
            out.println("<b>Sypnosis: </b>" + Movie.getSynopsis());
            out.println("</td>");
            out.println("</tr>");

            out.println("</table></CENTER>");
            out.println("</td>");
            out.println("</tr>");

            out.println("</table></CENTER>");

        }
    }
%>
<%@include file="footer.html"%>
